package com.lagou.server;

import com.lagou.model.Exam;
import com.lagou.model.User;

import java.io.*;
import java.util.List;

public class ServerFileList {
    private List<User> list;            //list 存放User类型的对象
    private List<Exam> list1;           //list1 存放Exam类型的对象

    //两个不同的
    public ServerFileList(List<User> list,List<Exam> list1) {
        this.list = list;
        this.list1 = list1;
    }

    /**
     * 从文件转为List集合, 默认开始启动服务器时，读取文件
     */
    public void fileToList()  {
        ObjectInputStream ois = null;
        ObjectInputStream ois1 = null;
        try {
            //文件转换为包含User对象的List集合
            File file = new File("D:/d.txt");
            if (file.length()!=0){
                ois = new ObjectInputStream(new FileInputStream("D:/d.txt"));
                for (User u : (List<User>) ois.readObject()){
                    list.add(u);
                }

                System.out.println("学员文件导入List成功");
                System.out.println(list);
            }else{
                System.out.println("文件是空的");
            }

            //文件转换为包含Exam对象的List集合
            File file1 = new File("D:/c.txt");
            if (file1.length()!=0){
                ois1 = new ObjectInputStream(new FileInputStream("D:/c.txt"));
                for (Exam e : (List<Exam>) ois.readObject()){
                    list1.add(e);
                }

                System.out.println("考题文件导入List成功");
                System.out.println(list1);
            }else{
                System.out.println("文件是空的");
            }


        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ois1 != null) {
                try {
                    ois1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }



    /**
     * 集合到文件的转换，关闭服务器时， 把list集合存放到文件中
     */
    public void listToFile(){
        ObjectOutputStream oos = null;
        ObjectOutputStream oos1 = null;
        try {
            //包含User的List集合输出到文件中
            oos = new ObjectOutputStream(new FileOutputStream("D:/d.txt"));
            oos.writeObject(list);
            System.out.println("学员List写入文件成功");

            //包含Exam的List集合输出到文件中
            oos1 = new ObjectOutputStream(new FileOutputStream("D:/c.txt"));
            oos.writeObject(list1);
            System.out.println("考题List写入文件成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != oos1) {
                try {
                    oos1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}